package com.smssalama;

import java.util.Vector;

import javax.microedition.lcdui.TextField;

/**
 * 
 * Provides miscellaneous utilities to the rest of the application.
 * 
 * @author Arnold P. Minde
 *
 */
public class Utils {
	/**
	 * Checks if a gien phonNumber is valid
	 * 
	 * @param phoneNumber - the phone number
	 * @return <code>true</code> if the phone number is valid, <code>false</code> otherwise.
	 *  
	 */
	public static boolean isValidaPhoneNumber(String phoneNumber) {
		if (phoneNumber == null || phoneNumber.length() == 0) {
			return false;
		}
		if (phoneNumber.equals("+")) {
			return false;
		}
		for (int i = 0; i < phoneNumber.length(); i++) {
			if (phoneNumber.charAt(i) == '+') {
				if (i > 0) {
					return false;
				} else {
					continue;
				}
			}
			if (!(phoneNumber.charAt(i) >= '0' && phoneNumber.charAt(i) <= '9')) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Prepends the + sign to a phone number input.
	 * The + sign may be missing because the user has forgotten to provided it.
	 * 
	 * <p>
	 * There have been a report that the + sign can not be entered in Blackberry when a field has {@link TextField#PHONENUMBER} constraint.
	 * If the + sign is pre-loaded, the whole form is messed up. So phone number fields should not be pre-loaded with the + sign, 
	 * and the number should be passed through this function before it is used.
	 * </p>
	 * 
	 * @param phone number number
	 * @return
	 */
	public static String fixPhoneNumberPrefix(String number) {
		if (number != null) {
			if (number.indexOf("+") < 0) {
				number = "+" + number;
			}
		}
		return number;
	}

	/**
	 * The list of supported languages should be provided in the <b>SMSSalama-Locales</b> property of the Midlet descriptor
	 * as a semicolon delimited list of locales such as:
	 * 	<code>en:English;sw:Swahili</code>.
	 * 
	 * @return an array of two dimension arrays with the first item being the locale id and the second the name. 
	 */
	public static String[][] getSupportedLocales() {
		String locales = SMSSalama.getInstance().getAppProperty(
				"SMSSalama-Locales");
		if(locales==null){
			System.out.print("WARNING: SMSSalama-Locales application property could not be read. Using default supported locales");
			return new String[][]{{"en", "English"}, {"sw","Kiswahili"}};
		}
		Vector entries = new Vector();
		while (true) {
			String current = null;
			int next = locales.indexOf(';');
			if (next >= 0) {
				current = locales.substring(0, next);
				locales = locales.substring(next + 1);
			} else {
				current = locales;
				locales = "";
			}
			if (current.length() != 0) {
				int splitIndex = current.indexOf(":");
				if (splitIndex > 0 && splitIndex < current.length() - 2) {
					String code = current.substring(0, splitIndex);
					String name = current.substring(splitIndex + 1);
					String[] entry = new String[] { code, name };
					entries.addElement(entry);
				}
			}
			if (locales.length() == 0) {
				break;
			}
		}
		// String[] sw = new String[]{"sw", "Kiswahili"};
		// String[] en = new String[]{"en", "English"};
		String[][] result = new String[entries.size()][];
		for (int i = 0; i < entries.size(); i++) {
			result[i] = (String[]) entries.elementAt(i);
		}
		return result;
	}
	/**
	 * Retrieve the port that SMS Salama should be listening for messages.
	 * 
	 * @return
	 */
	public static int getSMSSalamaPort(){
		int port = 50000;
		try {
			port = Integer.parseInt(SMSSalama.getInstance().getAppProperty("SMSSalama-Port"));
		} catch (Exception e) {
		}
		return port;		
	}
	/**
	 * Initialises and returns an {@link SMSTransport} instance.  
	 * 
	 * 
	 * @param listener
	 * @return
	 * @throws Exception
	 */
	/*
	public static SMSTransport initTransport(ISMSTransportListener listener)
			throws Exception {
		int port = getSMSSalamaPort();
		// String[] connections = PushRegistry.listConnections(false);
		SMSTransport transport = new SMSTransport(port, listener);
		transport.open();
		return transport;
	}
	*/
}
